﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Xps;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;
using System.Windows.Documents.Serialization;
using System.Runtime.InteropServices;
using System.IO;
using System.Xml;
using System.Threading;
namespace EncircleFTM
{
    public partial class Login : Form
    {
        #region "Member Variables"
        private ValidateLoginDelegate objValidateLoginDelegate;
        private System.IO.FileSystemWatcher FolderWatcher = null;
        #endregion
        #region "Constructor"
        /// <summary>
        /// Constructor
        /// </summary>
        public Login()
        {
            InitializeComponent();

        }



        #endregion
        #region "Events"
        /// <summary>
        /// Validate Login credentails
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //validate fields
                if (ValidateFields())
                {
                    //Validate with webservice
                    objValidateLoginDelegate = new ValidateLoginDelegate(ValidateLoginMethod);
                    EncircleFTM.EncircleDataUpload.UserDetails objLoginDetails = ValidateLoginMethod(txtUserName.Text, txtPassword.Text);

                    if (objLoginDetails != null)
                    {
                        //If valid user 
                        if (objLoginDetails.CompanyName != null)
                        {
                            clsSettings.CompanyName = objLoginDetails.CompanyName.ToString();
                        }
                        if (objLoginDetails.UniqueID != null)
                        {
                            clsSettings.UniqueID = objLoginDetails.UniqueID.ToString();
                        }
                        this.Close();
                        this.Update();
                        frmSettings objEncirclesettings = new frmSettings();
                        objEncirclesettings.BringToFront();
                        objEncirclesettings.Activate();

                        switch (objEncirclesettings.ShowDialog())
                        {
                            case DialogResult.OK:
                                clsSettings.ReadSettings();
                                break;
                            case DialogResult.Cancel:
                                break;
                        }

                       //clsCommonData.StartScheduler();
                        this.Update();

                        Clearfields();



                    }
                    else
                    {
                        //If invalid user
                        lblStatus.Text = "Invalid UserName/Password";
                        lblStatus.Visible = true;
                        Clearfields();
                    }
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.InnerException.Message.ToString();
            }
        }

        /// <summary>
        /// Exit Login Screen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFormClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
        #region "Delegates and methods"
        /// <summary>
        /// Clear Fields
        /// </summary>
        public void Clearfields()
        {
            try
            {
                txtUserName.Text = string.Empty;
                txtPassword.Text = string.Empty;
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.InnerException.Message.ToString();
            }
        }
        /// <summary>
        /// Validate Fields
        /// </summary>
        /// <returns></returns>
        public bool ValidateFields()
        {
            bool validate_fields = false;
            if (txtUserName.Text == string.Empty && txtPassword.Text == string.Empty)
            {
                validate_fields = false;
            }
            else
            {
                validate_fields = true;
            }
            return validate_fields;
        }
        /// <summary>
        /// Delegate for Validating login details
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public delegate EncircleDataUpload.UserDetails ValidateLoginDelegate(
           string username, string password);
        /// <summary>
        /// Method for validating login
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public EncircleDataUpload.UserDetails ValidateLoginMethod(string username, string password)
        {
            try
            {
                EncircleDataUpload.Service objEncircleService;
                EncircleDataUpload.UserDetails objEncircleUserDetails =
                   new EncircleFTM.EncircleDataUpload.UserDetails();
                //Step 4: create instance of EncricleDAtaupload webservice
                objEncircleService = new EncircleFTM.EncircleDataUpload.Service();
                if (clsSettings.URL == string.Empty)
                {
                    clsSettings.ReadSettings();
                    objEncircleService.Url = clsSettings.URL;
                }
                else
                {
                    objEncircleService.Url = clsSettings.URL;
                }

                //Step 5: create instance of Encircle Validate class
                objEncircleUserDetails = new EncircleFTM.EncircleDataUpload.UserDetails();


                objEncircleUserDetails = (EncircleFTM.EncircleDataUpload.UserDetails)
                       objEncircleService.ValidateLogin(username, password);
                return objEncircleUserDetails;
            }
            catch (Exception ex)
            {
                return null;
            }

        }
        #endregion

        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                btnSave.PerformClick();
            }
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                btnSave.PerformClick();
            }
        }

        private void Login_Shown(object sender, EventArgs e)
        {
            txtUserName.Focus();
        }
    }
}
